$(function(){
	editUserInfo();
	$("#BtnCancleAvatar").click(avatarCancle);
});
function updateCoords(c){
	$('#x').val(c.x);
	$('#y').val(c.y);
	$('#w').val(c.w);
	$('#h').val(c.h);
};
function ImageUpdate(){
	var height;
	var width;
	$("#TestImage").load(function(){
		$('#TestImage').Jcrop({
			onChange: updateCoords,
			onSelect: updateCoords,
			aspectRatio: 1
			},function(){
		        api = this;
		        api.setSelect([130,65,130+350,65+285]);
		        api.setOptions({ bgFade: true });
		        api.ui.selection.addClass('jcrop-selection');
		      }); 
		height=$(".jcrop-tracker:eq(1)").css("height");
		height=Math.floor(height.replace("px",""));
		width=$(".jcrop-tracker:eq(1)").css("width");
		width=Math.floor(width.replace("px",""));
		}
	);
	$("#BtnSaveAvatar").click(function(){
		var x=Math.floor($("#x").val());
		var y=Math.floor($("#y").val());
		var w=Math.floor($("#w").val());
		var h=Math.floor($("#h").val());
		var imagePath=$("#TestImage").attr("src");
		var param={
				"x":x,
				"y":y,
				"w":w,
				"h":h,
				"imagePath":imagePath,
				"height":height,
				"width":width
		};
		var urlStr="../image/updateImage.action";
		doAjaxHtml(urlStr,param,function(msg) {
			if (msg.indexOf("#") > 0) {
				var data = msg.split("#");
				var imagSrc=data[1].split("</pre>")[0];
				$(".curTou").html("<img src="+imagSrc+" id='TestImage'"+"/>");
				$(".curTou").attr("avatarPic",imagSrc);
				$("#BtnSaveAvatar").hide();
				$("#BtnCancleAvatar").hide();
				$("#change").show();
			}});
	});
}
function editUserInfo(){
	var url ="../user/editUserInfo.action";
	doAjaxHtml(url,null,function(response){
		$(".settingPage").html(response);
		var avatarPic=$(".curTou").attr("avatarPic");
		if(avatarPic.length>0){
			$(".curTou").html("<img src="+avatarPic+" id='TestImage'"+"/>");
		}
	});
}
function validateUserInfo(){
	var isRight=true;
	var nickname = $.trim($("#nickname").val());
	var email=$.trim($("#email").val());
	var trueName=$.trim($("#trueName").val());
	var summary=$.trim($("#summary").val());
	var websiteName=$.trim($("#websiteName").val());
	var websiteUrl=$.trim($("#websiteUrl").val());
	var birth=$.trim($("#birth").val());
	var pattern = /^\w+@\w+(\.\w+)+(\,\w+@\w+(\.\w+)+)*|\w+(\.\w+)+@\w+(\.\w+)+(\,\w+@\w+(\.\w+)+)*$/;
	var password1=$.trim($("#password1").val());
	var password2=$.trim($("#password2").val());
	if (!pattern.test(email)) {
		alertTipDown("请检查邮箱格式","cuo");
		isRight=false;
		return false;
	}
	if(nickname == ""){
		alertTipDown("需要你的姓名哦","cuo");
		isRight=false;
		return false;
	}else{
		if(nickname.length>20 || nickname.length<4){
			alertTipDown("昵称最少4、最多20个字符","cuo");
			isRight=false;
			return false;
		}
		var pattern = /^[a-zA-Z_0-9\u4E00-\u9FA5\s\\(\\)\\.]+$/;
		if (!pattern.test(nickname)) {
			alertTipDown("昵称格式不合法：中英文、数字、下划线","cuo");
			isRight=false;
			return false;
		}
	}
	if(trueName == ""){
		alertTipDown("需要你的姓名哦","cuo");
		isRight=false;
		return false;
	}
	if(trueName != ""){
		if(trueName.length>20){
			alertTipDown("姓名也太长了吧","cuo");
			isRight=false;
			return false;
		}
		var pattern = /^(?!\s)([\u4E00-\u9FA0]+$|^[a-z·\s]+)$/i;
		if (!pattern.test(trueName)) {
			alertTipDown("请输入有效的真实姓名","cuo");
			isRight=false;
			return false;
		}
//		var trueNameStr = new String(trueName);
//		if (trueNameStr.getBytesLength()< 4 || trueNameStr.getBytesLength()> 30) {
//			alert("请输入有效的真实姓名");
//			return false;
//		}
	}
	//校验url
	var strRegex = "^((https|http|ftp|rtsp|mms)?://)"
		+ "?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?"
		+ "(([0-9]{1,3}.){3}[0-9]{1,3}" 
		+ "|"
		+ "([0-9a-z_!~*'()-]+.)*"
		+ "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]."
		+ "[a-z]{2,6})"
		+ "(:[0-9]{1,4})?"
		+ "((/?)|"
		+ "(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$";
	var re=new RegExp(strRegex); 
	if(websiteUrl!=""){
		if (!re.test(websiteUrl)) {
			isRight=false;
			alertTipDown("请输入有效的url地址","cuo");
			return false;
		}
	}
	if(websiteName!=""){
		if(websiteName.length>40){
			isRight=false;
			alertTipDown("网站名也太长了吧","cuo");
			return false;
		}
	}
	if(summary!=""){
		if(summary.length>100){
			isRight=false;
			alertTipDown("个人简介也太长了吧","cuo");
			return false;
		}
	}
	if(password1!=""||password2!=""){
		var pattern = /^[A-Za-z0-9]+$/;
		if(pattern.test(password1)&&pattern.test(password2)){
			if (password1.length < 4||password2.length < 4) {
				isRight=false;
				alertTipDown("密码长度为4到16位","cuo");
				return false;
				}
			if (password1.length>16||password2.length>16) {
				isRight=false;
				alertTipDown("密码长度为4到16位","cuo");
				return false;
				}
		}
		else{
			isRight=false;
			alertTipDown("密码格式不对","cuo");
			return false;
		}
		if(password1!=password2){
			isRight=false;
			alertTipDown("两次输入密码不一致，请重新输入","cuo");
			return false;
		}
		if(password1==password2){
			var url="../userAjax/updatePassword.action";
			var params={
					"newPassword":password1
			};
			doAjax(url,params,null);
		}
	}
	if(isRight=true){
		saveUserInfo();
	}
	
}

/**
 * 保存基本信息
 */
function saveUserInfo(){
	var nickname = $.trim($("#nickname").val());
	var email=$.trim($("#email").val());
	var trueName=$.trim($("#trueName").val());
	var summary=$.trim($("#summary").val());
	var websiteName=$.trim($("#websiteName").val());
	var websiteUrl=$.trim($("#websiteUrl").val());
	var birth=$.trim($("#birth").val());
	var gender=$("#gender").val();
	var param = {"user.email":email,"user.trueName":trueName,"user.nickname":nickname,"user.summary":summary,"user.websiteName":websiteName,"user.websiteUrl":websiteUrl,
			"birthday":birth,"user.gender":gender};
	var requestUrl = "../userAjax/updateUserInfo.action";
	doAjax(requestUrl,param,function(ajaxResult){
		if(ajaxResult.OK){
			alertTipDown("修改成功","dui");
		}else{
			alertTipDown(ajaxResult.message,"cuo");
		}
	});
}
function uploadImage() {
	//$(".curTou").html("");
	//$(".curTou").html("图片预览区域");
	var options = {
		url : "../image/uploadTouXiang.action",
		type : "POST",
		dataType : "script",
		success : function(msg) {
			if (msg.indexOf("#") > 0) {
				var data = msg.split("#");
				var imagSrc=data[1].split("</pre>")[0];
				$("#TestImage").attr("src",data);
				ImageUpdate();
				$("#TestImage").load(function(){
					$("#select").hide();
					$("#BtnSaveAvatar").show();
					$("#BtnCancleAvatar").show();
				});
				
			}
		},error:function(e){
			var msg = e.responseText;
			if (msg.indexOf("#") > 0) {
				var data = msg.split("#");
				var imagSrc=data[1].split("</pre>")[0];
				//$(".curTou").html("");
				//$(".curTou").html("<img src="+imagSrc+ " id='TestImage'/>");
				$(".curTou").html("<img src="+imagSrc+" id='TestImage'"+"/>");
				ImageUpdate();
				
				//
				$("#select").hide();
				$("#BtnSaveAvatar").show();
				$("#BtnCancleAvatar").show();
			}
		}
	};
	$("#uploadImageForm").ajaxSubmit(options);
	return false;
}
//修改密码
function updatePassword(){
	var pattern = /^[A-Za-z0-9]+$/;
	var realPassword=$("#repassword").val();
	var password1=$("#password1").val();
	var password2=$("#password2").val();
	if(realPassword==""||password1==""||password2==""){
		alert("请输入密码");
		return false;
	}
	if(pattern.test(password1)&&pattern.test(password2)&&pattern.test(realPassword)){
		if (realPassword.length < 4||password1.length < 4||password2.length < 4) {
			alert("密码长度为4到16位");
			return false;
			}
		if (realPassword.length >16||password1.length>16||password2.length>16) {
			alert("密码长度为4到16位");
			return false;
			}
	}
	else{
		alert("密码格式不对");
		return false;
	}
	if(password1!=password2){
		alert("两次输入密码不一致，请重新输入");
		return false;
	}
	var url="../userAjax/updatePassword.action";
	var params={
			"realPassword":realPassword,
			"newPassword":password1
	};
	doAjax(url,params,function(ajaxResult){
		alert(ajaxResult.message);
	});
	
}

function avatarChange(){
	var obj = $(this);
	$('input[type="file"]').css('opacity',0);
	$(".curTou").html("图片预览区域");
	$("#change").hide();
	$("#select").show();
}

function avatarCancle(){
	var avatarPic=$(".curTou").attr("avatarPic");
	if(avatarPic.length>0){
		$(".curTou").html("<img src="+avatarPic+" id='TestImage'"+"/>");
	}
	else{
		$(".curTou").html("图片预览区域");
	}
	$("#BtnSaveAvatar").hide();
	$("#BtnCancleAvatar").hide();
	$("#change").show();
}
